import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from autotest.testsuites.test_base import TestBase
from autotest.pageobjects.demo_login_page import Login
import configparser
import os.path
config = configparser.ConfigParser()
path=os.path.dirname(os.path.abspath('.'))+'/autotest/config/'
config.read(path+'config.ini',encoding='utf-8')

class demoLoing(TestBase):

#测试登录成功
    def test_demo_login_suc(self):
        #这里一定要用test开头，把测试逻辑代码封装到一个test开头的方法里。
        input= Login(self.driver)
        #通过配置文件获取地址
        input.open(config.get('demo','URL'))
        time.sleep(5)
        input.value_username_input('superadmin')
        input.value_password_input('HqyMms@2025')
        input.check_password_click()
        input.submit_btn()
        time.sleep(2)
        input.get_windows_img()#调用基类截图方法
        try:
            #根据登录成功后跳转页面包含某个元素判断登录成功
            WebDriverWait(self.driver,5).until(EC.presence_of_element_located((By.ID,'doubleRowHeader')))
            print("test_demo_login_suc :test passed")
        except Exception as e:
            print("test_demo_login_suc :test failed",format(e))
        input.quit_browser()
    #密码错误测试
    def test_demo_login_fail(self):
        input2= Login(self.driver)
        input2.open(config.get('demo','URL'))
        time.sleep(5)
        input2.value_username_input('superadmin')
        input2.value_password_input('HqyMms@2026')
        input2.check_password_click()
        input2.submit_btn()#这里截图会报错
        time.sleep(2)
        try:
            WebDriverWait(self.driver, 5).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            assert alert.text=='密码错误'
            time.sleep(3)
            alert.accept()
            print("test_demo_login_fail :test passed",alert.text)
        except Exception as e:
            print("test_demo_login_fail:test failed",e)
        input2.quit_browser()